package com.unohacha.qqj.fzdr.ui.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qpg.superhttp.SuperHttp;
import com.qpg.superhttp.callback.SimpleCallBack;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.bean.BaseBean;
import com.unohacha.qqj.fzdr.bean.MyImgLibCate;
import com.unohacha.qqj.fzdr.ui.activity.MyImgLibDetialActivity;
import com.unohacha.qqj.fzdr.ui.activity.MyImgLibListActivity;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.fzdr.util.SpaceItemDecoration;
import com.unohacha.qqj.mylibrary.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static android.content.Context.MODE_PRIVATE;

/**
 * 我的图库碎片
 */
public class MyImgLibraryFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener{

    @BindView(R.id.mRefreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.recycleview)
    RecyclerView recycleview;
    private String type="1";
    private int a;
    private List<MyImgLibCate.Two> list,list1;
    SharedPreferences sharedPreferences;
    private String supplyCateId,imgUrl;

    @Override
    protected void initBundle(Bundle arguments) {
        type=arguments.getString("type");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_find_img_detial;
    }

    @Override
    protected void initView(View rootView) {
        list = new ArrayList<>();
        list1 = new ArrayList<>();
        sharedPreferences=getActivity().getSharedPreferences("locationInfo",MODE_PRIVATE);
//        imageGridLayout.setOnItemClickListener(this);
        WindowManager manager = getActivity().getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        a = outMetrics.widthPixels - 50;
        GridLayoutManager layoutManage = new GridLayoutManager(getContext(), 2);
        recycleview.setLayoutManager(layoutManage);
        recycleview.addItemDecoration(new SpaceItemDecoration(20));

//        getData();
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    @Override
    protected void initData() {

    }
    private class MyAdapter extends BaseQuickAdapter<MyImgLibCate.Two, BaseViewHolder> {

        public MyAdapter(int layoutResId, @Nullable List<MyImgLibCate.Two> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, MyImgLibCate.Two item) {
            ImageView imageView = (ImageView) helper.getView(R.id.supply_item_type_image);
            imageView.setMinimumWidth(a / 2);
            imageView.setMinimumHeight(a / 2);
            if(type.equals("1")){
                Glide.with(MyImgLibraryFragment.this)
                        .load(item.getPublic_pic())
                        //   .error(R.mipmap.ic_default_photo)
                        .override(a / 2, a / 2)
                        .into(imageView);
            }else {
                Glide.with(MyImgLibraryFragment.this)
                        .load(item.getPrivate_pic())
                        //   .error(R.mipmap.ic_default_photo)
                        .override(a / 2, a / 2)
                        .into(imageView);
            }

            helper.setText(R.id.tv_type_text,item.getName());
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if(type.equals("1")){
            if(list==null || list.size()==0){
                return;
            }
         //   Intent intent=new Intent(getContext(), MyImgLibDetialActivity.class);
            Intent intent=new Intent(getContext(), MyImgLibListActivity.class);
            intent.putExtra("typeid","1");
            intent.putExtra("id",list.get(position).getId()+"");
            startActivity(intent);
        }else {
            if(list==null || list.size()==0){
                return;
            }
            Intent intent=new Intent(getContext(), MyImgLibListActivity.class);
            intent.putExtra("typeid","2");
            intent.putExtra("id",list.get(position).getId()+"");
            startActivity(intent);
        }

    }
    private void getData(){
      //  showLoading();
        String token= Prefer.getInstance().getToken();
        SuperHttp.post("api/supplyinfo/supplyCate")
                .addParam("token", token)
                .addParam("typeid","2")
                .request(new SimpleCallBack<BaseBean<List<MyImgLibCate>>>() {
                    @Override
                    public void onSuccess(BaseBean<List<MyImgLibCate>> data) {
                        if(data.getCode()==0 || data.getData()==null){
                            return;
                        }
                     //   list.clear();
                        for(int i=0;i<data.getData().size();i++){
                            if(data.getData().get(i).getId()==1 ){
                                list.addAll(data.getData().get(i).getTwo());
                            }
                        }

                        MyAdapter adapter = new MyAdapter(R.layout.item_grid_my_img_lib, list);
                        adapter.setOnItemClickListener(MyImgLibraryFragment.this);
                        recycleview.setAdapter(adapter);
                        if (mRefreshLayout.isRefreshing()) {
                            mRefreshLayout.finishRefresh();
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                 //       hideLoading();
                    }
                });
    }
}
